define(['jquery', 'table'], function ($, Table) {

    var c = {
        test: function () {
            console.log('Hello World!');
        },
        // [{fieldcode:'x1'},{fieldcode:'x2'}]=>{'x1'=>{fieldcode:'x1'},'x2'=>{fieldcode:'x2'},}
        tool_getXcfieldjsonKeyObj(xcfieldjsonObj) {
            var that = this;
            xcfieldjsonKeyObj = [];
            for (var index in xcfieldjsonObj) {
                var item = xcfieldjsonObj[index];
                xcfieldjsonKeyObj[item.fieldcode] = item;
            }
            return xcfieldjsonKeyObj;
        },
        xcformcrud_getFormData: function (fieldfulljson) {
            var that = this;
            var data = {
                id: Config.id,
                xcform_id: Config.xcform.id,
            };
            for (var index in fieldfulljson) {
                var item = fieldfulljson[index];
                if (item["fieldcode"] != null && item["fieldcode"] != "") {
                    data["row[" + item["fieldcode"] + "]"] = item["value"];
                }
            }
            return data;
        },
        xcformcrud_saveFormData: function (baseurl, data, callbackFunc) {
            var that = this;
            Fast.api.ajax({
                url: Fast.api.fixurl(baseurl + "createOrUpdateXcformSaveAjax"),
                type: "post",
                data: data,
            }, function (res) {
                var result = callbackFunc(res);
                if (result === true) {
                    setTimeout(function () {
                        that.xcformcrud_openCreateOrUpdateXcformPage(false, baseurl, Config.xcform.id, res.id);
                    }, 300);
                }
                return result;
            });
        },
        xcformcrud_submitFormData: function (baseurl, data, callbackFunc) {
            var that = this;
            Fast.api.ajax({
                url: Fast.api.fixurl(baseurl + "createOrUpdateXcformSubmitAjax"),
                type: "post",
                data: data,
            }, function (res) {
                var result = callbackFunc(res);
                if (result === true) {
                    setTimeout(function () {
                        that.xcformcrud_openShowXcformPage(false, baseurl, Config.xcform.id, res.id);
                    }, 300);
                }
                return result;
            });
        },
        xcformcrud_openCreateOrUpdateXcformPage(isOpenNewPage, baseurl, xcformId, dataId) {
            var that = this;
            var url = Fast.api.fixurl(baseurl + 'createOrUpdateXcformPage?xcform_id=' + xcformId + "&id=" + dataId);
            if (isOpenNewPage === true) {
                Fast.api.open(url,"创建");
            } else {
                window.location = url;
            }

        },
        xcformcrud_openShowXcformPage(isOpenNewPage, baseurl, xcformId, dataId) {
            var that = this;
            var url = Fast.api.fixurl(baseurl + 'showXcformPage?xcform_id=' + xcformId + '&id=' + dataId+"&display_createbutton=Y");
            if (isOpenNewPage === true) {
                window.open(url);
            } else {
                window.location = url;
            }
        },
        listpage_renderViewTable(page_id, view_id, xcfieldjsonObj, showfields) {
            Fast.config.openArea = ['98%', '98%'];
            var that = this;
            Table.api.init({
                extend: {
                    index_url: 'csmitsm/xcflowviewpageviewdata/indexajax?view_id=' + view_id + "&page_id=" + page_id,
                    add_url: 'csmitsm/xcflowviewpageviewdata/createOrUpdateXcformPage?view_id=' + view_id + "&page_id=" + page_id,
                    edit_url: 'csmitsm/xcflowviewpageviewdata/showXcformPage?view_id=' + view_id + "&page_id=" + page_id,
                    // del_url: 'csmitsm/xcflowviewpageviewdata/del?view_id=' + view_id + "&page_id=" + page_id,
                    dragsort_url: 'ajax/weigh',
                    table: 'xcflowviewpageviewdata',
                }
            });
            var table = $("#table");

            var columns = [];

            var xcfieldjsonKeyObj = that.tool_getXcfieldjsonKeyObj(xcfieldjsonObj);

            for (var index in showfields) {
                var item = showfields[index];
                if (item['selected'] === "Y") {
                    var column = {
                        field: item.fieldcode,
                        title: item.title,
                    };
                    if (xcfieldjsonKeyObj[item.fieldcode]) {
                        switch (xcfieldjsonKeyObj[item.fieldcode]["fieldtag"]) {
                            case "xcform_control_input_datetime":
                                column["formatter"] = Table.api.formatter.datetime;
                                break;
                        }
                    }
                    columns.push(column);
                }
            }
            columns.push({
                field: 'operate',
                title: __('Operate'),
                table: table,
                events: Table.api.events.operate,
                formatter: Table.api.formatter.operate
            });

            table.bootstrapTable('destroy');
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [columns]
            });

            if (table.data("init") != "Y") {
                Table.api.bindevent(table);
            }
            table.data("init", "Y");
        }
    };
    return c;
});